package chap3_15;

import java.util.Arrays;
import java.util.Scanner;

public class Demo2 {
    public static void main(String[] args) {
        String[] menu = {"红烧带鱼", "鱼香肉丝", "时令蔬菜"};//菜品目录
        double[] price = {38.0, 20.0, 10.0};//对应单价
        int[] prais = {0, 0, 0};//对应点赞数
        int a = 10;
        String[] names = new String[a];// 保存订餐人名称
        String[] dishMegs = new String[a];// 保存菜品名
        int[] times = new int[a]; // 保存送餐时间
        String[] addresses = new String[a]; // 保存送餐地址
        int[] states = new int[a]; // 保存订单状态： 0：已预订 1：已完成
        double[] sumPrices = new double[a]; // 保存订单的总金额
        while (true) {
            System.out.println("欢迎使用“吃货联盟订餐系统");
            System.out.println("******************************");
            System.out.println("1、我要订餐");
            System.out.println("2、查看餐袋");
            System.out.println("3、签收订单");
            System.out.println("4、删除订单");
            System.out.println("5、我要点赞");
            System.out.println("6、退出系统");
            System.out.println("请选择：");
            Scanner in = new Scanner(System.in);
            int chosen = in.nextInt();
            if (chosen > 0 && chosen < 7) {
                switch (chosen) {
                    case 1: {
                        System.out.println("***我要订餐***");
                        System.out.println("请输入订餐人姓名：");
                        int dishchosen;//订餐编号
                        int num;//订餐份数
                        for (int j = 0; j < names.length; j++) {//输入订餐人姓名，检索第一个值为null的替代
                            if (names[j] == null) {
                                names[j] = in.next();
                                break;
                            }
                        }
                        System.out.print("序号   " + "菜名      " + "单价  " + "点赞数");
                        System.out.println();
                        for (int i = 0; i < menu.length; i++) {
                            System.out.println((i + 1) + "    " + menu[i] + "    " + price[i] + "    " + prais[i]);
                        }
                        System.out.println("请选择您要点的菜品编号（1-3）：");
                        while (true) {
                            dishchosen = in.nextInt();
                            if (dishchosen > 3 || dishchosen < 1) {
                                System.out.println("您选择的菜品有误，请选择1-3的数字");
                                continue;
                            }
                            break;
                        }
                        System.out.println("请选择您需要的份数：");
                        while (true) {
                            num = in.nextInt();
                            if (num <= 0) {
                                System.out.println("请输入大于0的数字");
                                continue;
                            }
                            break;
                        }
                        for (int i = 0; i < dishMegs.length; i++) {
                            if (dishMegs[i] == null) {
                                dishMegs[i] = (menu[dishchosen - 1] + num + "份");
                                break;
                            }
                        }
                        System.out.println("请输入送餐时间（时间是10点到20点间整点送餐）：");
                        int time;
                        while (true) {
                            time = in.nextInt();
                            if (time < 10 || time > 20) {
                                System.out.println("请输入10到20间的整数");
                                continue;
                            }
                            break;
                        }
                        for (int i = 0; i < times.length; i++) {
                            if (times[i] == 0) {
                                times[i] = time;
                                break;
                            }
                        }
                        System.out.println("请输入送餐地址：");
                        String adress = in.next();
                        for (int i = 0; i < addresses.length; i++) {
                            if (addresses[i] == null) {
                                addresses[i] = adress;
                                break;
                            }
                        }
                        System.out.println("订餐成功！");
                        System.out.println("您订的是:" + menu[dishchosen - 1] + num + "份");
                        System.out.println("送餐时间是：" + time + "点");
                        double sum = price[dishchosen - 1] * num;//餐费
                        double deliCharge = (sum >= 50) ? 0 : 5;
                        System.out.println("餐费：" + sum + "元," + "配送费：" + deliCharge + "元，" + "总计：" + (sum + deliCharge) + "元");
                        for (int i = 0; i < sumPrices.length; i++) {
                            if(sumPrices[0]==0){
                                sumPrices[i] = price[dishchosen - 1] * num + deliCharge;
                            }else if(names[i]==null){
                                sumPrices[i-1] = price[dishchosen - 1] * num + deliCharge;
                            }
                        }
                        while (true) {
                            System.out.println("输入0返回：");
                            String back = in.next();
                            break;
                        }
                        continue;
                    }
                    case 2: {
                        System.out.println("***查看餐袋***");
                        System.out.println("序号" + "\t  " + "订餐人\t  " + "产品信息\t" + "\t送餐时间\t" + "\t送餐地址\t" + "\t总金额\t" + "\t订餐状态");
                        for (int i = 0; i < names.length; i++) {
                            if (names[i] == null) {
                                break;
                            }
                            if (states[i] == 1) {
                                System.out.println((i + 1) + "\t  " + names[i] + "\t  " + dishMegs[i] + " \t " + times[i] + "点" + " \t " + addresses[i] + "\t\t" + sumPrices[i] + "元" + "\t\t" + "已完成");
                            } else if (states[i] == 0) {
                                System.out.println((i + 1) + "\t  " + names[i] + "\t  " + dishMegs[i] + " \t " + times[i] + "点" + " \t " + addresses[i] + "\t\t" + sumPrices[i] + "元" + "\t\t" + "已预订");
                            }
                        }
                        while (true) {
                            System.out.println("输入0返回：");
                            String back = in.next();
                            break;
                        }
                        continue;
                    }
                    case 3: {
                        System.out.println("***签收订单***");
                        System.out.println("请选择要签收的订单序号：");
                        int chosenum;
                        for (int i = 0; i < names.length; i++) {
                            while (true) {
                                chosenum = in.nextInt();
                                if (names[chosenum - 1] == null) {
                                    System.out.println("该订单不存在，请重新选择");
                                    continue;
                                }
                                if (states[chosenum - 1] == 1) {
                                    System.out.println("该订单已签收请重新选择");
                                    continue;
                                }
                                System.out.println("签收成功");
                                states[chosenum - 1] = 1;
                                break;
                            }
                            break;
                        }
                        while (true) {
                            System.out.println("输入0返回：");
                            String back = in.next();
                            break;
                        }
                        continue;
                    }
                    case 4: {
                        System.out.println("***删除订单***");
                        System.out.println("请输入要删除的订单号：");
                        int chosendelet;
                        for (int i = 0; i < names.length - 1; i++) {
                            while (true) {
                                chosendelet = in.nextInt();
                                if (names[chosendelet - 1] == null) {
                                    System.out.println("该订单不存在，请重新选择");
                                    continue;
                                }
                                int temp = 0;
                                String temp2 = null;
                                double temp3 = 0;
                                for (int k = chosendelet - 1; k < names.length - 1 - chosendelet; k++) {
                                    if (names[k] != null) {
                                        temp2 = names[k];
                                        names[k] = names[k + 1];
                                        names[k + 1]=temp2;
                                        temp2 = dishMegs[k];
                                        dishMegs[k] = dishMegs[k + 1];
                                        dishMegs[k + 1]=temp2;
                                        temp = times[k];
                                        times[k] = times[k + 1];
                                        times[k + 1]=temp;
                                        temp2 = addresses[k];
                                        addresses[k] = addresses[k + 1];
                                        addresses[k + 1]=temp2;
                                        temp = states[k];
                                        states[k] = states[k + 1];
                                        states[k + 1]=temp;
                                        temp3 = sumPrices[k];
                                        sumPrices[k] = sumPrices[k + 1];
                                        sumPrices[k + 1]=temp3;
                                        continue;
                                    }
                                    names[names.length - 1] = null;
                                    dishMegs[names.length - 1] = null;
                                    times[names.length - 1] = 0;
                                    addresses[names.length - 1] = null;
                                    states[names.length - 1] = 0;
                                    sumPrices[names.length - 1] = 0;
                                    break;
                                }
                                break;
                            }
                            break;
                        }
                        while (true) {
                            System.out.println("输入0返回：");
                            String back = in.next();
                            break;
                        }
                        continue;
                    }
                    case 5: {
                        System.out.println("***我要点赞***");
                        System.out.print("序号   " + "菜名      " + "单价  " + "点赞数");
                        System.out.println();
                        for (int i = 0; i < menu.length; i++) {
                            System.out.println((i + 1) + "    " + menu[i] + "    " + price[i] + "    " + prais[i]);
                        }
                        int admire;
                        System.out.println("请选择您要点赞的菜品序号：");
                        while (true) {
                            admire = in.nextInt();
                            if (admire < 0 || admire > 3) {
                                System.out.println("选择错误，请重新选择");
                                continue;
                            }
                            prais[admire - 1] = prais[admire - 1] + 1;
                            break;
                        }
                        while (true) {
                            System.out.println("输入0返回：");
                            String back = in.next();
                            break;
                        }
                        continue;
                    }
                    case 6: {
                        System.out.println("谢谢使用欢迎下次光临");
                        return;
                    }
                }
            } else {
                System.out.println("输入错误，请重新输入");
            }

        }
    }
}
